package net.wanho.shop.controller;


import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.wanho.shop.po.Attribute;
import net.wanho.shop.service.AttributeService;
import net.wanho.shop.vo.AjaxResult;
import net.wanho.shop.vo.AttributeVo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;


/**
 * <p>
 * 属性表 前端控制器
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Api(tags = "08.属性表接口")
@RestController
@RequestMapping("/attributes")
@RequiredArgsConstructor
public class AttributeController {

    private final AttributeService attributeService;

    @ApiOperation("属性表分页查询")
    @GetMapping("/list")
    public AjaxResult findPage(@ApiParam("页码") @RequestParam(defaultValue = "1") int pagenum, @ApiParam("页大小") @RequestParam(defaultValue = "5") int pagesize, @ApiParam("查询条件") Attribute attribute) {
        PageInfo<Attribute> pageInfo = attributeService.findPage(pagenum, pagesize, attribute);
        return AjaxResult.success(pageInfo);
    }

    @ApiOperation("根据主键查询属性表")
    @GetMapping("/{attrId}")
    public AjaxResult findById(@PathVariable Serializable attrId) {
        return AjaxResult.success(attributeService.findById(attrId));
    }

    @ApiOperation("新增属性表")
    @PostMapping
    public AjaxResult add(@RequestBody Attribute attribute) {
        attributeService.add(attribute);
        return AjaxResult.success();
    }

    @ApiOperation("修改属性表")
    @PutMapping("/{attrId}")
    public AjaxResult modify(@PathVariable Integer attrId,
                             @RequestBody Attribute attribute) {
        Attribute attribute2 = attributeService.modifyByAny(attrId, attribute);
        return AjaxResult.success(attribute2);
    }

    @ApiOperation("根据主键删除属性表")
    @DeleteMapping("/{attrId}")
    public AjaxResult removeById(@PathVariable Integer attrId) {
        attributeService.removeDeleteTime(attrId);
        return AjaxResult.success();
    }

    @ApiOperation("根据分类查询参数列表")
    @GetMapping("/category/{catId}")
    public AjaxResult getCategory(@PathVariable("catId") Integer catId, String sel) {
        List<AttributeVo> attributes = attributeService.selectByCatIdAndSel(catId, sel);
        return AjaxResult.success(attributes);

    }

    @ApiOperation("添加动态参数或静态属性")
    @PostMapping("/{catId}")
    public AjaxResult addAttr(@PathVariable("catId") Integer catId,
                              @RequestBody Attribute attribute) {
        attributeService.addAttr(catId, attribute);

        return AjaxResult.success();
    }
}
